﻿using AutoMapper;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Newtonsoft.Json;
using Warehouse.API.Applcation.Command.EnterStoragesCommand;
using Warehouse.Domain;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre;
using Warehouse.Infrstructre.Interface;
using WMSpublic;

namespace Warehouse.API.Applcation.CommandHandlers.EnterStoragesCommandHandler
{
    public class EnterStoragesUpdateCommandHandlers : IRequestHandler<EnterStoragesUpdateCommand, int>
    {
        protected readonly IEnterStoragesInfoRepsitory inforep;
        protected readonly IEnterStoragesRepsitory entrep;
        protected readonly IMapper mapper;
        protected readonly EFCoreDBcontext db;

        public EnterStoragesUpdateCommandHandlers(IEnterStoragesInfoRepsitory inforep, IEnterStoragesRepsitory entrep, IMapper mapper, EFCoreDBcontext db)
        {
            this.inforep = inforep;
            this.entrep = entrep;
            this.mapper = mapper;
            this.db = db;
        }

        public async Task<int> Handle(EnterStoragesUpdateCommand request, CancellationToken cancellationToken)
        {
            using (var tran = db.Database.BeginTransaction(System.Data.IsolationLevel.Serializable))
            {
                try
                {
                    //修改入库单
                    var s = mapper.Map<StockRoom>(request.dtos);
                    await entrep.UpdateAsync(s);
                    //查询入库产品信息
                    var infolist = (await inforep.GetAllAsyns()).Where(x => x.StockId == request.dtos.StockId);
                    //先删除原有入库产品信息
                    foreach (var item in infolist)
                    {
                        await inforep.RemoveAsync(item);
                    }
                    //再添加新的入库产品信息
                    foreach (var item in request.dtos.rkcp)
                    {
                        StockProducts sp = new StockProducts()
                        {
                            StockId = s.StockId,
                            ProductsID = item.ProductsID,
                            Batch = item.Batch,
                            Sprice = item.Sprice,
                            Quantity = item.Quantity,
                            Binlocation = item.Binlocation,
                            StateSP = 1,
                            ShenHePeople = item.ShenHePeople,
                            ShenHeTime = item.ShenHeTime,
                            office_id = s.office_id,
                            user_id = item.user_id,
                            AddName = s.AddName,
                            AddTime = item.AddTime,
                            DelName = item.DelName,
                            DelTime = item.DelTime,
                            IsDel = item.IsDel,
                            UpdName = s.UpdName,
                            UpdTime = DateTime.Now
                        };
                        await inforep.AddAsync(sp);
                    }
                    MessageCreateDto a = new MessageCreateDto();
                    a.AddName = "张三";
                    a.AddTime = DateTime.Now;
                    a.Description = "入库修改";
                    a.Title = "入库修改";
                    a.CurrenTime = DateTime.Now;
                    a.MessageType = "修改";
                    a.MessageState = false;
                    a.Office_Id = "1";
                    await MessageAdds(a);
                    tran.Commit();
                    return 1;
                }
                catch (Exception)
                {
                    //回滚事务
                    tran.Rollback();
                    throw;
                }
            }

        }
        public async Task<int> MessageAdds(MessageCreateDto a)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyNCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMSIsImp0aSI6IjQzMzA2NmRmLWYyNjAtNDI2Ny1iYWY5LWJkMzgxZGVhZGMwYiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiLmnLHms4rpnJYiLCJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy91cmkiOiIiLCJpc3MiOiJSZW5hb2JvMjAwNCIsImF1ZCI6IldlaWZ1d3UyMjAzQSJ9.E0PG5CbU-_2efHW14aTC5dmRc3FC7BLU0TIgjNsA1wo";
            HttpClientWrapper client = new HttpClientWrapper(token);
            string url = "http://116.205.121.197:8000/xxx/api/MessageCreate/CreateMessage";
            //将a转换为json字符串
            string jsonStr = JsonConvert.SerializeObject(a);
            string postRep = await client.PostAsync(url, jsonStr);
            return 1;
        }
        }
    
}
